package com.Sake.web.controller.Home;

import com.Sake.common.annotation.LoggerManage;
import com.Sake.common.constant.Constants;
import com.Sake.common.utils.ip.HttpUtils;
import com.Sake.system.service.HomeImageService;
import com.Sake.web.Service.Home.HomeMessageService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 主页全信息获取更改
 *
 * @author zsh
 * @date 2021.7.6
 */
@RestController
@RequestMapping("/Home/Message")
public class HomeMessageController {


    @Autowired
    private HttpUtils httpUtils;

    /**
     * 主页图片Service
     */
    @Autowired
    private HomeMessageService homeMessageService;
    /**
     * 主页图片Service
     */
    @Autowired
    private HomeImageService homeImageService;


    @GetMapping(value = "/getHomeMessage", produces = "application/json", consumes = "application/json")
    @LoggerManage(moduleName = "主页信息", option = "获取所有主页资料")
    public JSONObject getHomeMessage() {
        try {
            return homeMessageService.getHomeMessage();
        } catch (Exception e) {
            throw new RuntimeException("主页信息  -  获取所有主页资料  -  异常", e);
        }
    }

    @PostMapping(value = "/editHomeMessage", produces = "application/json", consumes = "application/json")
    @LoggerManage(moduleName = "主页信息", option = "修改主页资料")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    public JSONObject modifyHomepageInformation(@RequestBody JSONObject data) {
        try {
            String head_name = data.getString("hander_name");
            String personalDeclaration = data.getString("personalDeclaration");
            String bottomInformation = data.getString("bottomInformation");
            String login_bottomInformation = data.getString("login_bottomInformation");
            return homeMessageService.modifyHomepageInformation(head_name, personalDeclaration, bottomInformation, login_bottomInformation);
        } catch (Exception e) {
            throw new RuntimeException("主页信息  -  获取所有主页资料  -  异常", e);
        }
    }

    @GetMapping(value = "/getHomeDynamic", produces = "application/json", consumes = "application/json")
    @LoggerManage(moduleName = "主页信息", option = "获取动态")
    public JSONObject getHomeDynamic() {
        try {
            return homeMessageService.getHomeDynamic();
        } catch (Exception e) {
            throw new RuntimeException("主页信息  -  获取动态  -  异常", e);
        }
    }

    @PostMapping(value = "/addHomeDynamic", produces = "application/json", consumes = "application/json")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @LoggerManage(moduleName = "主页信息", option = "添加动态")
    public JSONObject initiateADynamic(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //获取登录用户id
            Object requestAttribute = request.getAttribute(Constants.JWT_USERID);
            if (requestAttribute == null) {
                throw new RuntimeException("未登录用户不得添加动态");
            }
            int userid = Integer.parseInt(requestAttribute.toString());
            //获取当前登录IP
            String ip = httpUtils.getRemortIp(request);
            String content = data.getString("content");
            String contentImages = data.getString("contentImages");
            return homeMessageService.initiateADynamic(userid, ip, content, contentImages);
        } catch (Exception e) {
            throw new RuntimeException("主页信息  -  添加动态  -  异常", e);
        }
    }

    @PostMapping(value = "/updetImage", produces = "application/json", consumes = "multipart/form-data")
    @LoggerManage(moduleName = "主页信息", option = "添加动态图片上传")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    public JSONObject updetImage(@RequestParam("file") MultipartFile contentImage) {
        try {
           return homeImageService.updetImage(contentImage);
        } catch (Exception e) {
            throw new RuntimeException("主页图片设置  -  多图修改  -  异常", e);
        }
    }

    @PostMapping(value = "/delectUmage", produces = "application/json", consumes = "multipart/form-data")
    @LoggerManage(moduleName = "主页信息", option = "删除单个上传的图片")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    public JSONObject delectUmage(@RequestBody JSONObject data) {
        try {
            String imageUrl = data.getString("imageUrl");
            return homeImageService.delectUmage(imageUrl);
        } catch (Exception e) {
            throw new RuntimeException("主页图片设置  -  多图修改  -  异常", e);
        }
    }

}
